package com.example.birdfriends.service.impl;

import com.example.birdfriends.dao.UserDao;
import com.example.birdfriends.pojo.user.User;
import com.example.birdfriends.pojo.user.UserForm;
import com.example.birdfriends.service.UserService;
import com.example.birdfriends.utils.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    Result result = null;
    // 验证信息，验证码，邮箱，密码和确认密码是否一致
    @Override
    public Map<String, Object> verifyRegister(UserForm userForm, HttpSession session){
        String email = (String) session.getAttribute("email");
        String code = (String) session.getAttribute("code");
        long startTime = (long) session.getAttribute("startTime");

        String codeForm = userForm.getCode();
        if(email == null || email.isEmpty()){
            // 邮箱不为空
            return result.resultMap(false, "注册失败！邮箱不能为空！！！", null);
        }else if(!code.equals(codeForm)){
            // 验证码一致
            return result.resultMap(false, "注册失败！验证码错误！！！", null);
        }else if(!userForm.getPassword().equals(userForm.getRepeatedPwd())){
            // 密码要和确认密码一致
            return result.resultMap(false, "注册失败！确认密码错误！！！", null);
        }
        long endTime = System.currentTimeMillis();  // 验证验证码的到期时间
        long intervalMinutes = ((endTime - startTime)/1000)/60;
        session.removeAttribute("startTime");
        if(intervalMinutes > 10){  // 验证码不能超出时间
            return result.resultMap(false, "注册失败！验证超时！！！", null);
        }

        return result.resultMap(true, "验证通过", null);
    }

    // 验证登录验证码
    @Override
    public Map<String, Object> verifyLogin(UserForm userForm, HttpSession session){
        String initialCode = (String) session.getAttribute("verify_code");
        String code = userForm.getCode();
        System.out.println("验证码对比：" + initialCode+ " " + code);
        if(initialCode == null || !initialCode.equalsIgnoreCase(code)){
            // session的验证码与前端传输的验证码不同
            return result.resultMap(false, "验证码错误", null);
        }

        return result.resultMap(true, "验证通过", null);
    }

    @Override
    public boolean isExist(UserForm userForm) {
        // 用户注册，首先验证用户是否已经存在
        String email = userForm.getEmail();
        User user = userDao.findUserByEmail(email);
        return user != null;  // 判断用户是否存在
    }

    @Autowired
    private UserDao userDao = null;

    @Override
    @Transactional
    public int insertUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public User findUserByEmailAndPassword(String email, String password) {
        return userDao.findUserByEmailAndPassword(email, password);
    }

    @Override
    public int logoff(Long id) {
        return userDao.deleteUser(id);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int updatePassword(String newPwd, String email) {
        return userDao.updatePassword(newPwd, email);
    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.findUserByEmail(email);
    }

    @Override
    public List<Long> findAllUserIds() {
        return userDao.findAllUserIds();
    }

    @Override
    public User findUserByUid(Long uid) {
        return userDao.findUserById(uid);
    }

    @Override
    public List<User> findUsersByName(String name) {
//        System.out.println("搜索名称："+name);
        return userDao.findUsersByName(name);
    }

    @Override
    public Long countUserNumber() {
        return userDao.countUserNumber();
    }

    @Override
    public List<Long> findUserIdsOrderById() {
        return userDao.findUserIdsOrderById();
    }
}
